package com.cyandpos.pad.common.ManagerCardManager;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.common.swipemanager.SwipeCardCommon;
import com.cyandpos.pad.http.OkHttpUtils;
import com.google.gson.JsonObject;

/**
 * Created by Administrator on 2017/05/16.
 */

public class SwipManagerCardModel implements SwipeCardCommon.DoAfterSwipeCard, IManagerWay {
    private Context mContext;
    private String orderNo;
    private OnResultGLK glk;

    public SwipManagerCardModel(String orderNo,Context context, OnResultGLK glk) {
        this.orderNo = orderNo;
        this.mContext = context;
        this.glk = glk;
    }


    private String retCode;

    public void check(String content) {
//        content = "113";
        PosHttpManager.managerYZHttpEexcute(orderNo,content, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<MangerCardData> baseResponse = BaseResponse.fromJson(response.toString(),MangerCardData.class);
                if(baseResponse.getCode().equals("0")){
                    glk.onResult(true, baseResponse);
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        glk.onResult(false, baseResponse.getMsg());
                    }else {
                        glk.onResult(false, baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                glk.onResult(false, msg);
            }
        });
    }


    @Override
    public void doAfterSwipeCard(boolean isSuccess, String result, int handId) {
        if (isSuccess) {
            check(result);
        } else {
            Toast.makeText(mContext, result + "", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void swipe() {
        Log.i("9527", "swipe");
        Toast.makeText(mContext, "刷卡", Toast.LENGTH_SHORT).show();
        SwipeCardCommon swipeCard = new SwipeCardCommon(mContext);
        swipeCard.setReceptor(this, 0);
        swipeCard.startSwipe();
    }

    @Override
    public void inputNum(String content) {
        Log.i("9527", "inputNum");
        if (!content.isEmpty()) {
            check(content);
        }
    }

    public interface OnResultGLK {
        void onResult(boolean isSuccess, Object data);
    }


}
